<template>
	<view>
		<!-- 头部导航 -->
		<u-navbar title="出售管理" :title-bold="true" title-color="#000" :border-bottom="false" custom-class="home-bg" :background="{ background: 'transparent' }"></u-navbar>

		<view class="items">
			<view v-for="(item, index) in ['我的', '出售记录']">
				<view @click="sort(index)" style="display: flex; flex-direction: column; align-items: center">
					<text :class="[sortIndex == index ? 'avater' : 'item']">{{ item }}</text>
					<view :class="[sortIndex == index ? 'border-bottom' : '']"></view>
				</view>
			</view>
		</view>

		<view v-if="loading" class="add-load">
			<u-loading mode="flower"></u-loading>
		</view>

		<view v-else-if="fields.length != 0">
			<view v-if="sortIndex == 0">
				<ProductList :listingBool="true" @str-show="showListing" :productList="fields" backColor="transparent"></ProductList>
				<loadMore :currentLength="fields.length" :total="total" :pageSize="pageSize" @load-list="getMybankPage(sortIndex)" ref="loadArray" @load-add="pageNum++"></loadMore>
			</view>

			<view v-else-if="sortIndex == 1">
				<recordListing :orders="fields"></recordListing>
				<loadMore :currentLength="fields.length" :total="total" :pageSize="pageSize" @load-list="getMybankPage(sortIndex)" ref="loadArray" @load-add="pageNum++"></loadMore>
			</view>
		</view>

		<u-empty v-else icon-size="400" src="/static/main/image2x.png"></u-empty>

		<u-popup v-model="show" :closeable="true" mode="center">
			<view class="show-body">
				<view class="top-text">
					<u-icon color="red" name="error-circle"></u-icon>
					温馨提醒
				</view>
				<view class="str">{{ strBool ? '当前商品已结束挂售~' : '当前商品不在出售时间段哦' }}~</view>
				<button @click="close()">我知道了</button>
			</view>
		</u-popup>

		<template v-if="appointShow">
			<u-popup border-radius="30" v-model="appointShow" mode="bottom" length="386rpx">
				<view class="order-body">
					<view class="name">{{ itemField.goodName }}</view>
					<view class="product-section">
						<view class="product-image-container">
							<image :src="path + itemField.goodImg.split(',')[0]" mode="aspectFill"></image>
						</view>

						<view class="product-details">
							<view class="product-title">{{ itemField.title }}</view>
							<u-rate :value="itemField.axGood.goodStars" disabled size="28" active-color="#F88725" gutter="0"></u-rate>

							<view class="quantity">数量：{{ itemField.totalNum }}</view>
							<view class="right-bottom">
								<text>
									<span>￥</span>
									{{ itemField.price * itemField.totalNum }}
								</text>
							</view>
						</view>
					</view>
					<view class="btn">
						<button class="left-btn" @click="appointClose">取消出售</button>
						<button class="right-btn" @click="setSellGood()">确认出售</button>
					</view>
				</view>
			</u-popup>
		</template>

		<u-back-top :scroll-top="scrollTop"></u-back-top>
	</view>
</template>

<script>
import user from '@/api/user';
import api from '@/api/index.js';
import ProductList from '@/components/produc-list/index.vue';
import recordListing from '@/components/record-listing/record.vue';
import loadMore from '@/components/load-more/load-more.vue';
const SortType = {
	MY_LIST: 0,
	MY_LIST_RECORD: 1
};

export default {
	components: {
		ProductList,
		recordListing,
		loadMore
	},
	data() {
		return {
			loading: true,
			show: false,
			appointShow: false,
			sortIndex: 0,
			scrollTop: 0,
			strBool: false, //是否在抢购时间
			fields: [],
			total: 0,
			pageNum: 1,
			pageSize: 10,
			itemField: {},
			path: api.getPath()
		};
	},
	onPageScroll(e) {
		this.scrollTop = e.scrollTop;
	},
	onLoad(o) {
		this.sort(o.sortIndex);
	},
	onReachBottom() {
		if (this.total > this.pageNum * this.pageSize) {
			this.pageNum++;
			this.$refs.loadArray.loadMore();
		}
	},
	methods: {
		async sort(row = 0) {
			this.pageNum = 1;
			this.sortIndex = row;
			if (row == SortType.MY_LIST) {
				await this.getMylist();
			} else if (row == SortType.MY_LIST_RECORD) {
				await this.getMylistRecord();
			}
		},
		close() {
			this.show = false;
		},
		showListing(item) {
			this.itemField = item;
			if (true) {
				//委拍
				this.appointShow = true;
			} else {
				//不再时间范围内
				this.show = true;
			}
		},
		appointClose() {
			this.appointShow = false;
		},
		async getMylist() {
			let _this = this;
			_this.loading = true;
			const res = await user.getMylist({
				pageNum: _this.pageNum,
				pageSize: _this.pageSize
			});
			_this.fields = await res.rows.map((item) => {
				item.goodImg = item.axGood.goodImg;
				item.goodName = item.axGood.goodName;
				return item;
			});
			_this.loading = false;
			_this.total = res.total;
		},
		getMybankPage(row) {
			let _this = this;
			if (row == SortType.MY_LIST) {
				user.getMylist({
					pageNum: _this.pageNum,
					pageSize: _this.pageSize
				}).then((res) => {
					_this.fields = [
						..._this.fields,
						...res.rows.map((item) => {
							item.goodImg = item.axGood.goodImg;
							item.goodName = item.axGood.goodName;
							return item;
						})
					];

					_this.total = res.total;
				});
			} else if (row == SortType.MY_LIST_RECORD) {
				user.getMylistRecord({
					pageNum: _this.pageNum,
					pageSize: _this.pageSize
				}).then((res) => {
					_this.fields = [..._this.fields, ...res.rows];
					_this.total = res.total;
				});
			}

			return this.fields.length;
		},
		async getMylistRecord() {
			let _this = this;
			_this.loading = true;
			const res = await user.getMylistRecord({
				pageNum: _this.pageNum,
				pageSize: _this.pageSize
			});
			_this.fields = res.rows;
			_this.loading = false;
			_this.total = res.total;
		},
		setSellGood() {
			user.setBuy({
				orderId: this.itemField.id
			}).then((res) => {
				uni.showToast({
					title: '出售成功',
					icon: 'success'
				});
				this.sort(this.sortIndex);
				this.appointClose();
			});
		}
	}
};
</script>

<style lang="scss" scoped>
.order-body {
	margin: 30rpx 60rpx 0 60rpx;

	.name {
		font-weight: 550;
		font-size: 32rpx;
		color: #333333;
		margin-bottom: 20rpx;
	}
	.btn {
		display: flex;
		justify-content: center;
		margin-top: 50rpx;
		button {
			width: 188rpx;
			height: 56rpx;
			border-radius: 70rpx 70rpx 70rpx 70rpx;
			font-size: 28rpx;
			line-height: 58rpx;
		}
		.left-btn {
			color: #777777;
			background: #eeeeee;
			margin-right: 50rpx;
		}
		.right-btn {
			color: #ffffff;
			background: #f05a39;
		}
	}

	.product-section {
		display: flex;
		border-radius: 4px;
		overflow: hidden;
		.product-image-container {
			image {
				width: 178rpx;
				height: 144rpx;
				border-radius: 20rpx;
			}
		}

		.product-details {
			flex: 1;
			padding-left: 16px;
			display: flex;
			flex-direction: column;
			justify-content: space-evenly;

			.right-bottom {
				display: flex;
				align-items: center;
				text {
					font-family: MiSans, MiSans;
					font-weight: 500;
					font-size: 30rpx;
					color: #f05a39;
					text-align: center;
					font-style: normal;
					text-transform: none;
					span {
						font-size: 23rpx;
					}
				}
			}
			.product-title {
				font-weight: 550;
				font-size: 26rpx;
				color: #333;
				display: -webkit-box;
				-webkit-line-clamp: 1; /* 限制为两行 */
				-webkit-box-orient: vertical;
				overflow: hidden;
				text-overflow: ellipsis;
			}

			.quantity {
				font-size: 20rpx;
				color: #777777;
				margin-bottom: 10rpx;
			}
		}
	}
}

.show-body {
	background: linear-gradient(180deg, #ffd8cf 0%, #fff 50%);
	width: 558rpx;
	height: 360rpx;
	border-radius: 35rpx;
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: space-evenly;
	.top-text {
		font-weight: 400;
		font-size: 40rpx;
		color: #333333;
	}
	.str {
		font-size: 28rpx;
		color: #333333;
	}
	button {
		width: 274rpx;
		height: 60rpx;
		background: #f05a39;
		border-radius: 64rpx 64rpx 64rpx 64rpx;

		font-weight: 500;
		font-size: 32rpx;
		color: #ffffff;
		line-height: 60rpx;
	}
}
.items {
	display: flex;
	justify-content: space-around;
	margin: 30rpx 45rpx;
	line-height: 60rpx;
	.item {
		font-weight: 400;
		font-size: 28rpx;
		color: #333333;
	}
	.border-bottom {
		width: 30rpx;
		height: 8rpx;
		background: #f05a39;
		border-radius: 50rpx 50rpx 50rpx 50rpx;
	}
	.avater {
		font-weight: 550;
		font-size: 28rpx;
		color: #333333;
	}
}
</style>
